﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIPanel : MonoBehaviour
{
    public UIManager UiManager;
    public bool isActive;
    public bool isAllowRightClickClose = true;

    protected IEnumerable OnClickGenerate()
    {
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
        {
            position = Input.mousePosition
        };

        List<RaycastResult> raycastResults = new List<RaycastResult>();
        EventSystem.current.RaycastAll(pointerEventData, raycastResults);

        foreach (var result in raycastResults)
        {
            yield return result;   
        }
    }

    public UIManager GetUiManager()
    {
        if (UiManager == null)
        {
            UiManager = FindObjectOfType<UIManager>().gameObject.GetComponent<UIManager>();
        }

        return UiManager;
    }

    public virtual void CloseWithRightClick()
    {
        if (isActive && isAllowRightClickClose && Input.GetMouseButtonUp(1))
        {
            // 让UI管理器来关闭当前面板
            CloseCurrentPanel();
        }
    }

    public virtual void CloseCurrentPanel()
    {
        UiManager.CloseCurrentPanel();
    }

    public virtual void MovePanelWithAnimation(Vector3 targetPosition, float duration)
    {
        transform.DOLocalMove(targetPosition, duration);
    }
}
